package com.cps116.friendstracker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.easy.facebook.android.apicall.FQL;
import com.easy.facebook.android.error.EasyFacebookError;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class QueryActivity_CheckinsOfFriendActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queryactivity_checkinsoffriend_layout);

		FQL facebookFQL = ((FriendsTrackerApplication) getApplication()).getFacebookFQL();
		ArrayList<String> friends = (ArrayList<String>)((FriendsTrackerApplication) getApplication()).getUser().getFriends();
		String[] friendsArray = ((FriendsTrackerApplication) getApplication()).getUser().getFriendsArray();

		ListView list=(ListView)findViewById(R.id.queryactivity_checkinsoffriend_query_listview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, friendsArray);
		list.setAdapter(adapter);

		list.setOnItemClickListener(new ListOptionSelector(this, facebookFQL, friends));

	}

	private class ListOptionSelector implements OnItemClickListener
	{
		Context context;
		FQL facebookFQL;
		ArrayList<String> friends;

		ListOptionSelector(Context c, FQL f, ArrayList<String> fr)
		{
			context = c;
			facebookFQL = f;
			friends = fr;
		}

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			ArrayList<String> friendCheckins = new ArrayList<String>();

			//display this particular friend's check-ins
			Pattern p = Pattern.compile(",(.+)");
			Matcher m = p.matcher(friends.get(position));
			String s = "";

			if(m.find())
			{
				try {
					s = facebookFQL.fqlQuery("SELECT name FROM place WHERE page_id " +
							"IN (SELECT page_id FROM checkin WHERE author_uid = " + m.group(1) + ")");
				} 
				catch (EasyFacebookError e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				p = Pattern.compile("<name>(.+?)</name>");
				m = p.matcher(s);

				while(m.find())
				{
					friendCheckins.add(m.group(1));
				}

				String[] result;

				if(friendCheckins.isEmpty())
				{
					result = new String[1];
					result[0] = "No Check-ins";
				}
				else
				{
					result = new String[friendCheckins.size()];

					for(int i = 0; i < friendCheckins.size(); i++)
					{
						result[i] = friendCheckins.get(i);
					}
				}

				Intent intent = new Intent(context, QueryActivity_CheckinsOfFriendActivity2.class);
				intent.putExtra("display-info", result);
				startActivity(intent);

			}

		}
	}



	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}


}
